package com.closeenough.app;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class MarkersOverlay extends ItemizedOverlay {

    private MainWindowActivity activity = null;
    private List<OverlayItem> markersOnMap = new ArrayList<OverlayItem>();
    private OverlayItem positionItem;
    private OverlayItem meetingItem;
    private Paint radiusPaint;

    public MarkersOverlay(Drawable marker, MainWindowActivity activity) {
        super(boundCenterBottom(marker)); 
        this.activity = activity;
        radiusPaint = new Paint();
        radiusPaint.setARGB(25, 0, 0, 255);
        radiusPaint.setAntiAlias(true);
        //HACK: para evitar una excepcion en la API si la lista de overlays esta vacia
        drawMarker(new GeoPoint(0, 0), "","");
    }

    public boolean onTap (final GeoPoint p, final MapView mapView){
        boolean tapped = super.onTap(p, mapView);
        //meaning, if a marker wasn't tapped
        if (!tapped)
        {
    		updateMeetingMarker(p);
        }                   
        return true;
    }

    //Return true in order to say that the listener has correctly taken the event into account 
    @Override
    protected boolean onTap(int index) {
        return true;
    }

    private OverlayItem drawMarker(GeoPoint p, String title, String snippet ) {        
            OverlayItem overlayitem = new OverlayItem(p, title, snippet);
            markersOnMap.add(overlayitem);
            populate();
            return overlayitem;
    }

    public void updatePositionMarker(GeoPoint p)
    {
    	markersOnMap.remove(positionItem);
    	positionItem = drawMarker(p, "Current Location","Here I am!");
    }
    
    public void updateMeetingMarker(GeoPoint p)
    {
    	removeMeetingMarker();
		meetingItem = drawMarker(p, "Meeting Point", "Here we'll meet!");
		activity.addProximityAlert(p);
    }

	public void removeMeetingMarker() {
		markersOnMap.remove(meetingItem);
	}
        
    public List<OverlayItem> getOverlays() {
        return markersOnMap;
    }

    public void addOverlay(OverlayItem overlay) {
        markersOnMap.add(overlay);
    }
    
    @Override
    protected OverlayItem createItem(int i) {
        return markersOnMap.get(i);
    }

    @Override
    public int size() {
        return markersOnMap.size();
    }
    
    public GeoPoint getMeetingGeoPoint()
    {
    	return meetingItem.getPoint();
    }
    
	public boolean hasMeetingPoint()
	{
		return (meetingItem != null);
	}

    @Override
	public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow)
    {
    	if(meetingItem != null && markersOnMap.contains(meetingItem))
    	{
    		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
    		int meetingRadius = Integer.parseInt(sharedPref.getString("radius", "500"));
    		Point p = GeoPointHelper.getPoint(meetingItem.getPoint(), mapView);
	    	int drawingRadius =  GeoPointHelper.getPixelDistance(meetingItem, mapView, meetingRadius);
	    	canvas.drawCircle(p.x, p.y, drawingRadius, radiusPaint);
    	}
    	super.draw(canvas, mapView, shadow);
    }
}